package com.rockcent.mall.lib.helper;

import com.rockcent.mall.lib.domain.Location;
import com.rockcent.mall.lib.dto.LocationDto;
import com.rockcent.mall.lib.repository.LocationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by zhangjh on 2016/11/15.
 */
@Component
public class LocationHelper {

    @Autowired
    private LocationRepository locationRepository;

    public LocationDto getFullLocation(Long locationId){
        LocationDto locationDto=new LocationDto();
        Location loc = locationRepository.findOne(locationId);
        if(loc!=null){
        int level = loc.getLevel();
            if (level == 1) {
                locationDto.setProviceId(loc.getId());
                locationDto.setProviceName(loc.getLocationName());
            }
        if (level == 3) {
            Location city = locationRepository.findOne(new Long(loc.getParentId()));
            Location provice = locationRepository.findOne(new Long(city.getParentId()));
            locationDto.setCityId(city.getId());
            locationDto.setCityName(city.getLocationName());
            locationDto.setProviceId(provice.getId());
            locationDto.setProviceName(provice.getLocationName());
            locationDto.setLocationName(loc.getLocationName());
            locationDto.setId(loc.getId());


        } else if (level == 2) {
            Location provice = locationRepository.findOne(new Long(loc.getParentId()));
            Location city = loc;
            locationDto.setCityId(city.getId());
            locationDto.setCityName(city.getLocationName());
            locationDto.setProviceId(provice.getId());
            locationDto.setProviceName(provice.getLocationName());
        }
        }
        return locationDto;
    }
}
